package com.luo.bbs.ums.webapi.config;


import com.luo.bbs.ums.webapi.filter.SSOFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Autowired
    private SSOFilter ssoFilter;

    String[] urls = {"/kaptcha/img","/kaptcha/check/**",
            "/user/sso/login","/user/registry","/user/checkValue"
    };
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()//要求请求必须被授权
            .antMatchers(urls)//匹配一些路径
            .permitAll()//允许访问
            .anyRequest()//除以上配置以外的请求
            .authenticated();//经过认证的

        http.formLogin();
        http.csrf().disable(); //禁用 ‘防止伪造跨域攻击’
        http.cors();
        http.addFilterBefore(ssoFilter, UsernamePasswordAuthenticationFilter.class);

    }
}
